

function close_preview()
{
	$('[preview]').fadeOut('fast',function()
	{
		window.in_preview = false;
		$('[preview]').each(function()
		{
			this.parentNode.removeChild(this);
		});
		window.alerted_image = false;
	});
$(document).bind('contextmenu',onmlay);
}


function preview(url)
{
	window.in_preview = true;
	try { clearTimeout(window.rename_timer); $('#rename_div').stop().css('display','none');} catch(e) { }
	var bg = document.createElement('div');
	var page = get_page_size();
	$(bg).css(
		{
			height: page.height,
			width: page.width,
			zIndex: '999',
			display:'none',
			position:'absolute',
			top:'0px',
			left:'0px',
			padding:'0px',
			margin:'0px'
		})
		.fadeTo(1,0,function(){ this.style.display = ''; })
		.attr('id','preview_bg')
		.attr('preview','yes')
		.bind('click',close_preview);
		
//$(document).bind('onContextmenu', 'return false');
document.oncontextmenu = function() { return false; };
$(document).unbind('contextmenu');
	
	$(document.body).append(bg);
	$(bg).css('background-color','#000000').fadeTo(70,0.6);
	
	var div = document.createElement('div');
	$(div)
		.css(
		{
			height: '100px',
			width: '100px',
			zIndex: '1000',
			backgroundColor: '#fff',
			position:'absolute',
			top:'0px',
			left:'0px',
			padding:'0px',
			margin:'0px'
		})
		.attr('preview','yes');
	
	
	
	$(div)
		.attr('id','preview_img_div')
		.attr('img_src',url)
		.css(
			{ 
				top: $(document).scrollTop()+50, 
				left: $(document).scrollLeft() + ($(window).width() / 2) -50
			})
		.fadeIn()
		.bind('click',function(event){ event.returnValue = false; return false; });
	$(document.body).append(div);

	window.preview_worked = false;
	var imgobj = new Image();
	$(imgobj)
	.bind('load',preview_img_load)
	.bind('error',function()
	{ 
		if (!window.alerted_image) 
			alert(' {IMAGE_LOAD_ERROR} '); 
		window.alerted_image = true; 
		setTimeout('window.alerted_image = false;',5000)
		close_preview(); 
	})
	.attr('id','preview_hidden_img')
	.attr('src',url)
	.css('display','none')
	.attr('preview','yes')
	
	
	$(document.body).append(imgobj);
	if (imgobj.width>0 && imgobj.height>0 ) preview_img_load.call(imgobj);

}


function preview_img_load()
{
	if (window.preview_worked) return;
	window.preview_worked = true;
	var _width = this.width;
	var _height = this.height;
	var p = 0;
	if (_height > 480)
	{
		p = 480/_height;
		_height = 480;
		_width = p*_width;
	}
	if (_width >640 )
	{
		p = 640/_width;
		_width = 640;
		_height = p*_height;
	}
	this.height = _height;
	this.width = _width;
	window._this_img_width = _width;
	window._this_img_height = _height;
	
	var div_width = parseInt(this.width) + 20;
	var div_height = parseInt(this.height) + 20;

	$('#preview_img_div')
		.animate({ width: div_width , left: $(document).scrollLeft() + ($(window).width() - div_width ) / 2 },700)
		.animate({ height: div_height }, 700 ,function()
		{ 
			var page = get_page_size();
			$('#preview_bg').css( { height:page.height,width:page.width });
			var img = document.createElement('img');
			img.style.margin = "10px";
			img.src = $('#preview_img_div').attr('img_src');
			img.width = window._this_img_width;
			img.height = window._this_img_height;
			$(img).fadeTo(1,0,function()
			{
				$('#preview_img_div').append(this).find('img').fadeTo('fast',1, function()
				{
					var page = get_page_size();
					$('#preview_bg').css( { height:page.height,width:page.width });
					var img = document.createElement('input');
					$(img)
						.css(
						{
							position:'absolute',
							width:70,
							zIndex:4000,
							height:25,
							left:$('#preview_img_div').offset().left+$('#preview_img_div').width() -70,
							top:$('#preview_img_div').offset().top -25,
							cursor:'pointer'
						})
						.bind('click',function()
						{
							close_preview();
						})
						.attr('value',"{PREVIEW_CLOSE}")
						.attr('preview','yes')
						.attr('type','button');
					document.body.appendChild(img);
					$(img).fadeIn('fast',function()
					{
						var page = get_page_size();
						$('#preview_bg').css( { height:page.height,width:page.width });
					});
				});
			});
		});
}
